/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.appointment.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.service.CrudService;
import cn.knet.sp.ncrm.common.utils.DateUtils;
import cn.knet.sp.ncrm.modules.customer.appointment.dao.PotentialAppointmentInfoDao;
import cn.knet.sp.ncrm.modules.customer.appointment.entity.PotentialAppointmentInfo;
import cn.knet.sp.ncrm.modules.customer.potentialcustomer.entity.PotentialCustomer;
import cn.knet.sp.ncrm.modules.customer.visitrecord.entity.VisitRecord;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

import javax.annotation.Resource;

/**
 * 预约信息Service
 * @author 冯亚鹏
 * @version 2015-09-08
 */
@Service
@Transactional(readOnly = true)
public class PotentialAppointmentInfoService extends CrudService<PotentialAppointmentInfoDao, PotentialAppointmentInfo> {

	@Resource
	private PotentialAppointmentInfoDao potentialAppointmentInfoDao;

	public PotentialAppointmentInfo get(String id) {
		return super.get(id);
	}
	
	public List<PotentialAppointmentInfo> findList(PotentialAppointmentInfo potentialAppointmentInfo) {
		return super.findList(potentialAppointmentInfo);
	}
	
	public Page<PotentialAppointmentInfo> findPage(Page<PotentialAppointmentInfo> page, PotentialAppointmentInfo potentialAppointmentInfo) {
		Page<PotentialAppointmentInfo> infoPage = super.findPage(page, potentialAppointmentInfo);
		List<PotentialAppointmentInfo> infoList = infoPage.getList();
		for(PotentialAppointmentInfo info:infoList){

			if("1".equals(potentialAppointmentInfo.getStatus())){
				info.setStatus("已经结束");
			}else if("2".equals(potentialAppointmentInfo.getStatus())){
				info.setStatus("即将开始");
			}else if("3".equals(potentialAppointmentInfo.getStatus())){
				info.setStatus("尚未开始");
			}else{
				long thisTime = System.currentTimeMillis();
				if(null==info.getAppointmentDate()||info.getAppointmentDate().getTime()>thisTime+3*24*60*60*1000){
					info.setStatus("尚未开始");
				}else if(info.getAppointmentDate().getTime()>=thisTime&&info.getAppointmentDate().getTime()<=thisTime+3*24*60*60*1000){
					info.setStatus("即将开始");
				}else if(info.getAppointmentDate().getTime()<=thisTime){
					info.setStatus("已经结束");
				}
			}

		}

		return infoPage;
	}
	
	@Transactional(readOnly = false)
	public void save(PotentialAppointmentInfo potentialAppointmentInfo) {
		super.save(potentialAppointmentInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(PotentialAppointmentInfo potentialAppointmentInfo) {
		super.delete(potentialAppointmentInfo);
	}


	/**
	 * 创建 预约信息，并处理
	 *
	 * @param potentialCustomer        the potential customer
	 * @param potentialAppointmentInfo the potential appointment info
	 * @author fengyapeng
	 * @since 2015 /09/07 17:56:25
	 */
	public void createAppointmentInfo(PotentialCustomer potentialCustomer, VisitRecord visitRecord,
									  PotentialAppointmentInfo potentialAppointmentInfo) {

		if (potentialAppointmentInfo != null && potentialAppointmentInfo.getAppointmentDate() != null) {
			potentialAppointmentInfo.setPotentialCustomerId(potentialCustomer.getId());
			potentialAppointmentInfo.setVisitRecordId(visitRecord.getId());
			potentialAppointmentInfo.setPotentialCustomerId(potentialCustomer.getId());
			potentialAppointmentInfo.setCreateUserId(UserUtils.getUser().getId());
			potentialCustomer.setDateAppointment(potentialAppointmentInfo.getAppointmentDate());// 客户表记录 预约记录
			// 提前单位,分钟
			potentialAppointmentInfo.setNoticeDate(DateUtils.addMinutes(potentialAppointmentInfo.getAppointmentDate(),
																		0 - potentialAppointmentInfo.getAdvanceMin().intValue()));
			this.save(potentialAppointmentInfo);
		}
	}

	/**
	 * 获取待提醒的预约回访条数
	 *
	 * @param potentialAppointmentInfo
	 * @author fengyapeng
	 * @since 2015 /09/07 17:56:25
	 */
	public int getRemindNum(PotentialAppointmentInfo potentialAppointmentInfo) {
		return potentialAppointmentInfoDao.getRemindNum(potentialAppointmentInfo);
	}
}